﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;
using GTauto.Site.FrontEnd.AppCode.Helpers;
using System.Linq;

namespace GTauto.Site.FrontEnd.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {


            // DALModelModification.Singleton.Test(0);
           // Membership.CreateUser("admin", "12345", "admin@mail.ru");
            // dfg dfgd g
            return View();
        }

        public ActionResult ChangeRegion(int id)
        {
            HttpCookie cookie = new HttpCookie("CurrentRegion");
            cookie.Value = id.ToString();
            cookie.Expires = DateTime.Now.AddYears(1);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

            return RedirectToAction("Index");
        }


        public ActionResult Regions()
        { 
            List<GTRegion> regionsGetPaged = null;
            
            if(HttpContext.Cache["regionsCash"]==null)
            {
                HttpContext.Cache["regionsCash"] = DALKladr.Singleton.RegionsGetPaged();
            }

            regionsGetPaged = (List<GTRegion>) HttpContext.Cache["regionsCash"];


            GTRegion curReg = null;

            if (HttpContext.Request.Cookies.Get("CurrentRegion") == null)
            {

                string serverVariable = HttpContext.Request.ServerVariables["REMOTE_ADDR"];

                //serverVariable = "178.206.53.55";

               
                if (HttpContext.Cache[serverVariable + "_region"] == null)
                {
                    string resp =
                        PostHelper.HttpPost(
                            string.Format("http://geoip.elib.ru/cgi-bin/getdata.pl?ip={0}&hex=3ffd", serverVariable), "");
                    try
                    {
                        using (XmlReader reader = XmlReader.Create(new StringReader(resp)))
                        {
                            reader.ReadToFollowing("GeoAddr");
                            reader.ReadToFollowing("Town");
                            string city = reader.ReadElementContentAsString();
                            reader.ReadToFollowing("Region");
                            string region = reader.ReadElementContentAsString();

                            if (region != null)
                            {
                               var temp =
                                    regionsGetPaged.SingleOrDefault(x => x.FullName.ToLower() == region.ToLower());

                               HttpCookie cookie = new HttpCookie("CurrentRegion");
                               cookie.Value = temp.Id.ToString();
                               cookie.Expires = DateTime.Now.AddYears(1);
                               System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                               
                                HttpContext.Cache[serverVariable + "_region"] = temp;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                curReg = (GTRegion)HttpContext.Cache[serverVariable + "_region"];
            }
            else
            { 
                string value = HttpContext.Request.Cookies.Get("CurrentRegion").Value;
                var temp = regionsGetPaged.SingleOrDefault(x => x.Id== Convert.ToInt32(value));
                curReg = temp;
            }


            if(curReg==null)
            {
                ViewBag.regions = new SelectList(regionsGetPaged, "Id", "Name");
            }
            else
            {
                ViewBag.regions = new SelectList(regionsGetPaged, "Id", "Name",curReg.Id);
            }

            return View();
        }

    }
}
